//跳转到下一个页面需要发送的参数

function clickAction(videoSourceId) {
	var thisvideoSourceId = videoSourceId;
	window.location.href="http://127.0.0.1:8020/gongchengjie/video.html?videoSourceId="+thisvideoSourceId+"";		
}



$(function(){
	
	function getChapterName(i) {
		var chapterName = "";
		i = i + 1;
		switch(i){
			case 1: 
			chapterName = "第一章";
			break;
			case 2: 
			chapterName = "第二章";
			break;
			case 3: 
			chapterName = "第三章";
			break;
			case 4: 
			chapterName = "第四章";
			break;
		}
		return chapterName;
	}
	

	function sectionName(i) {
		var sectionName = "";
		i = i + 1;
		switch(i){
			case 1: 
			sectionName = "第一章";
			break;
			case 2: 
			sectionName = "第二章";
			break;
			case 3: 
			sectionName = "第三章";
			break;
		}
		return sectionName;
	}
	
	
	//展示星星
	function stars(score){	
			var w=8;	
			var totalw=w*score;		
			return totalw+'px';
	}
	
	
	//获取上一个页面的数据
	function GetRequest() {
		var url = location.search; //获取url中"?"符后的字串 
		var theRequest = new Object();
		if (url.indexOf("?") != -1) {
			var str = url.substr(1);
			strs = str.split("&");
			for (var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
			}
		}
		return theRequest;
	}
	var Request = new Object();
	Request = GetRequest();
	//alert(Request['id']);
	//alert(Request['id']);
	//用来让下一个页面获取上一个页面的参数
	
	var urll = window.location.href;
	console.log(GetRequest(urll));
	
	
	

	//转换年-月-日
 function getLocalTime(nS) {     
    return new Date(parseInt(nS)).toLocaleString().substr(0,10).replace("/","-").replace("/","-");      
}     

	
	$(function(e) {
	   var $area = $('.search-area'),
	        $showhide = $('.showhide-search'),
	        $input = $('.search-input');      
	    $('.course-nav-hd').on('click', '.showhide-search', function (event){
	        var isShow = $(this).attr('data-show');
	        if( isShow == 'no'){
	            show()
	            $input.focus();
	            $input.keyup(function(){
	            	$(".search-result").css("display","block");
	            });
	            $(".search-input").css("border","1px solid #bcbcbc");
	        }else{
	        	if($input.val()==""){
	        		hide()
	        	}
	        }                    
	        return false;
	    })        
	    $('.course-nav-hd').on('click', '.search-area', function (event){
	        return false;
	    })         
	    $(document).on('click', function (){
	        if( $input.val() != ''){
	            hide()
	        }else{
	        	hide()
	        }
	    })       
	    var show = function (){
	        $showhide.attr('data-show', 'yes')
	        $area.show(1, function (){ $area.removeClass('min') })
	    }     
	    var hide = function (){
	        $showhide.attr('data-show', 'no')
	        $area.fadeOut('slow', function (){
	            $area.addClass('min')
	        })
	    }
		})
	
	//评价星际
    var stepW = 24;
    var stars = $("#star > li");
    $("#showb").css("width",0);
    stars.each(function(i){
        $(stars[i]).click(function(e){
            var n = i+1;
            $("#showb").css({"width":stepW*n});
            $(this).find('a').blur();
            return stopDefault(e);
        });
    });
	function stopDefault(e){
	    if(e && e.preventDefault)
	           e.preventDefault();
	    else
	           window.event.returnValue = false;
	    return false;
	}
	
	
	var stepW = 24;
    var stars = $("#star_1 > li");
    $("#showb_1").css("width",0);
    stars.each(function(i){
        $(stars[i]).click(function(e){
            var n = i+1;
            $("#showb_1").css({"width":stepW*n});
            $(this).find('a').blur();
            return stopDefault(e);
        });
    });
	function stopDefault(e){
	    if(e && e.preventDefault)
	           e.preventDefault();
	    else
	           window.event.returnValue = false;
	    return false;
	}
	
	var stepW = 24;
    var stars = $("#star_2 > li");
    $("#showb_2").css("width",0);
    stars.each(function(i){
        $(stars[i]).click(function(e){
            var n = i+1;
            $("#showb_2").css({"width":stepW*n});
            $(this).find('a').blur();
            return stopDefault(e);
        });
    });
	function stopDefault(e){
	    if(e && e.preventDefault)
	           e.preventDefault();
	    else
	           window.event.returnValue = false;
	    return false;
	}
	
	var stepW = 24;
    var stars = $("#star_3 > li");
    $("#showb_3").css("width",0);
    stars.each(function(i){
        $(stars[i]).click(function(e){
            var n = i+1;
            $("#showb_3").css({"width":stepW*n});
            $(this).find('a').blur();
            return stopDefault(e);
        });
    });
	function stopDefault(e){
	    if(e && e.preventDefault)
	           e.preventDefault();
	    else
	           window.event.returnValue = false;
	    return false;
	}
	
	//发表评论 
		$(".textarea").on({
			'focus':function(){
				if($(".pl-limit").html()>300){
					$(".pl-input-warp").css("border","1px solid red");
				}
				else{
					$(".pl-input-warp").addClass("pl-input-warps");
					$(".pl-input-warp").css("border","1px solid green");
				}
				
			},
			'keyup':function(){		
				//$(".pl-input-warp").css("border","1px solid #d3dce0");
				var num=300-$(".textarea").val().length;
				if(num>=0){
					$(".pl-limit").html(num);
					//$(".pl-input-warp").css("border","1px solid #d3dce0");
				}else{
					$(".pl-limit").html(Math.abs(num)+300);
					$(".pl-input-warp").css("border","1px solid red");
					return false;
				}
				
				if($(".pl-limit").html()<300){  		  	
					$(".pl-input-warp").css("border","1px solid #d3dce0");
				}
		},
		'blur':function(){
			if($(".pl-limit").html()>300){  		  	
				$(".pl-input-warp").css("border","1px solid red");
			}else{
			  	$(".pl-input-warp").css("border","1px solid #d3dce0");
			  	$(".pl-input-warp").removeClass("pl-input-warps");
			}
		}
	});
   		    
	$(".input-btn-pl").click(function(){
		var $val=$(".textarea").val();
		var $val_length=$(".textarea").val().length;
		if($val == ""){
			$(".pl-input-warp").css("border","1px solid red");
			return false;
		}else if($val_length>300){
			return false;
		}else{
			alert("这里写成功后的东西");
		}
	})

	//视频缩进切换
	var slide_flag = 10; //计数器
	$("body").on('click','.aaa',function(){	
		if(slide_flag == 10) { //第一次点击
			$(".video-list").animate({'right':'0'});
		    $(".video-warp").animate({'width':'77.5%'});
		    $(".aaa").css({"background":"#434343 url(img/course_next.png) no-repeat center center"});
			slide_flag = 12;		
		}else if(slide_flag == 12) { //第二次点击
			$(".video-list").animate({'right':'-305'});
	        $(".video-warp").animate({'width':'100%'});
	       	$(".aaa").css({"background":"#434343 url(img/course_prev.png) no-repeat center center"});
			slide_flag = 10;		
		}
	});
	
	//已关注
	var $follow=1;
	$(".follow").on('click',function(){
		if($follow==1){
			$(this).html("已关注").css({"background":"#ececec"});
			$(this).css("color","#666");
			$follow++;
		}else if($follow!=1){		
			$(this).html("+ 关注").css({"background":"#49b4e1"});
			$(this).css("color","#fff");
			$follow=1;
		}
	})
	
	//免费视频的选项卡切换
	$(".price-left-tab ul li").click(function(){
		$(this).addClass("active").siblings().removeClass("active");
		var _index=$(".price-left-tab ul li").index(this);
		$(".price-left-bd").eq(_index).show().siblings().hide();
	})
	
	//立即试学跳转
	$(".pay-trys").click(function(){
		 $('html,body').animate({'scrollTop':690},500);
		 $(".task-part-list").children("a:first").children().addClass("active");
		 $("div.active").children("i:first").remove();
	})
	
	//付费视频           
	$(".task-part-item").click(function(){	
		if($(this).hasClass('task-disable')){
			//$(".task-part-item").attr('disabled',"true");
			$(this).parent().removeAttr('href');
		}
	})
	
	$(".album").on('click',function(){
		$(".new-re").css("display","block")
	})
	$(".album-course").on('click',function(){
		$(".new-re").css("display","none")
	})
	
	//立即试学跳转
	$(".pay-trys").click(function(){
		 $('html,body').animate({'scrollTop':690},500);
		 $(".task-part-list").children("a:first").children().addClass("active");
		 $("div.active").children("i:first").remove();
	})
	
	//付费视频           
	$(".task-part-item").click(function(){	
		if($(this).hasClass('task-disable')){
			//$(".task-part-item").attr('disabled',"true");
			$(this).parent().removeAttr('href');
		}
	})
	
	$(".album").on('click',function(){
		$(".new-re").css("display","block")
	})
	$(".album-course").on('click',function(){
		$(".new-re").css("display","none")
	})
	
	
		$.ajax({		 
			type:"GET",
		    dataType:"json",
		    url:'http://192.168.0.12:8080/elink_video/course/getCourseContent/1',
		    //url:"star.json",
		    success: function(result) {
			  	if(result.code==1000){
			  		var items = result.data.course;
	  		  		$('<div/>',{	      		  			
	  		  			html:
	  		  			'<div class="nav-box">'+
							'<div class="course-nav-hd clearfix">'+						
								'<span class="blod">'+
									'<a href="javascript:;">全部课程'+
									'</a>'+
								'</span>'+
								'>'+
								'<span class="step">'+
									'<a href="javascript:;">'+items.courseId+
									'</a>'+
								'</span>'+
								'>'+
								'<span class="step">'+
									'<a href="javascript:;">'+items.childTypeId+
									'</a>'+
								'</span>'+
								'>'+
								'<span class="step">'+
									'<a href="javascript:;">'+items.courseName+
									'</a>'+
								'</span>'+									
								'<div class="module-warp fr">'+
									'<a href="course.html" class="module">视频专区'+
									'</a>'+
									'<a href="identification.html" class="module">视频上传'+
									'</a>'+
								'</div>'+
								'<div class="search-warp clearfix">'+
					            	'<div class="search-area min" data-search="top-banner">'+
					               	 	'<input class="search-input"  placeholder="请输入想搜索的内容..." type="text" autocomplete="off">'+
					               	 	  '<ul class="search-result" style="display: none;">'+
						               	 		'<li>高等数学'+
						               	 		'</li>'+
						               	 		'<li>大学物理'+
						               	 		'</li>'+
						               	 		'<li>高等数学'+
						               	 		'</li>'+
						               	 		'<li>大学物理'+
						               	 		'</li>'+
						               	 		'<li class="case">清空搜索选项'+						               	 		
						               	 		'</li>'+
					               	 		'</ul>'+
					            	'</div>'+
					           		'<div class="showhide-search" data-show="no">'+
					           			'<i class="icon-search">'+
					           			'</i>'+
					           		'</div>'+
					        	'</div>'					
	  		  		}).appendTo('.'+'nav-box').addClass("course-nav-hd clearfix");
			  	}			  		
		    },
		     error:function(jqXHR){     
		       console.log("发生错误：" + jqXHR.status)  
		    }
	}) 
		     
		    //视频
			$.ajax({		 
				type:"GET",
			    dataType:"json",
	   			//url:'http://192.168.0.12:8080/elink_video/videoSource/getVideoSource?videoSourceId='+1+'&seed='+getAuth().seed+'&authToken='+getAuth().authToken+'',
			    url:'http://192.168.0.12:8080/elink_video/videoSource/getVideoSource?videoSourceId=30&seed=c95265c429fb70de11ba4b3010cf43d0&authToken=33052ecc2cf0874635fc6e6f93e282df',
			    //url:"star.json",
			    success: function(result) {
				  	if(result.code==0){
				  	var items = result.data;
				  		  //alert(items.score);

				alert(result.data.videoUrl);					
						$("#example_video_1_html5_api").append(
				//console.log('<source src="http://192.168.0.12:8080/elink_video/videoSource/'+result.data.videoUrl+' type="video/mp4" id="video"/>')
				'<source src="http://192.168.0.12:8080/elink_video/'+result.data.videoUrl+'"type=video/mp4 id="video"/>'
				);
				
				$(".video-content").append(
						'<div class="video-list">'+
							'<div class="text-right">'+
								'<h2>'+items.videoSourceName+
								'</h2>'+
						    '<div class="score clearfix">'+
								'<span>综合评分</span>'+							
								'<div class="fivestars" id="fivestars">'+
									'<div class="autostars" id="autostars" style="width:'+20+'">'+
//									'<div class="autostars" id="autostars" style="width:'+stars(items.score)+'">'+
									'</div>'+
								'</div>'+
							'</div>'+
								'<div class="text-msg">'+
									'<span class="frequency"> 播放 <em>'+items.playTimes+
									'</em> 次 </span>'+
									'<span class="collect">收藏 <em>'+items.collectionTimes+
									'</em></span>'+
							    '</div>'+
								'<span class="prices">免费</span>'+
								'<p>'+items.introduction+
								'</p>'+
								'<span class="free-go">去评价</span>'+
					    '</div>'+
						'<ul class="open">'+
							'<li class="aaa"></li>'+
						'</ul>'+
				'</div>'
				);				
			}			  		
			    },
			     error:function(jqXHR){     
			       console.log("发生错误：" + jqXHR.status)  
			    } 
		
		});
		
		
		
		
		$(".abstract").on('click',function(){			
			$.ajax({ 
				type:"GET",
			    dataType:"json",
			    url:'http://192.168.0.12:8080/elink_video/course/getCourseContent/1',
			    //url:"star.json",
			    success: function(result) {
					if(result.code==1000){
			  		var items = result.data.course;			  		
		  		  		$('<div/>',{	      		  			
		  		  			html:
							'<div class="price-left-bd">'+
								'<p class="price_1">'+items.describes+
								'</p>'+
							'</div>'		  		  			
		  		  		}).appendTo('.'+'price-left-content');
					}
			    },
			     error:function(jqXHR){     
			       console.log("发生错误：" + jqXHR.status)  
			    } 
		});
	})
		
		//简介列表
	//$(".abstract").on('click',function(){			
			$.ajax({ 
				type:"GET",
			    dataType:"json",
			    url:'http://192.168.0.12:8080/elink_video/course/getCourseContent/1',
			    //url:"star.json",
			    success: function(result) {
					if(result.code==1000){
			  		var items = result.data.course;			  		
		  		  		$('<div/>',{	      		  			
		  		  			html:
							'<div class="price-left-bd">'+
								'<p class="price_1">'+items.describes+
								'</p>'+
							'</div>'		  		  			
		  		  		}).appendTo('.'+'price-left-content');
					}
			    },
			     error:function(jqXHR){     
			       console.log("发生错误：" + jqXHR.status)  
			    } 
		});
	//})
		
		//章节列表
	//$(".chapter").on('click',function(){//	 
			$.ajax({ 
				type:"GET",
			    dataType:"json",
			    url:'http://192.168.0.12:8080/elink_video/course/getCourseChapterSection/1',
				//url:"json(2).json",
			    success: function(result) {

					var items=result.data;
					//console.log(items);
					//console.log(items[i].chapterName)

					for (var i = 0; i< items.length; i++) {
						//console.log("这是外面的1：");
//						removeChildNodes(document.getElementById("dela"));
						$('<div/>',{	      		  			
	  		  			html:	  		  			
	  		  			'<h3>'+getChapterName(i)+":"+items[i].chapterName+
	  		  			'</h3>'					
	  		  		}).appendTo('.'+'price-left-bd:nth-child(2)').addClass("task-chapter").append(     
			       		'<div class="task-part-list">'+
			       		'</div>'	  
		    			);
	  		  		
//	  		  			$(".task-chapter:nth-child(2)")
						//生成新节点 章 做节点处理
						//console.log(items[i]);
						//console.log(items[i].chapterName);
						var sectionData = items[i].sectionData;
						//console.log(items[i]);
						//alert(sectionData.length);
						for (var j = 0; j < sectionData.length; j++){
							//console.log("这是里面的：11111"+sectionData.length);
//							removeChildNodes(document.getElementById("dela"));
							//生成新的子节点 
							//console.log(sectionData[j]);
							//console.log("开始"+i+"---"+j+"------"+sectionData[j].sectionName)							
							//console.log(sectionData[j].videoSourceId);
							//?videoSourceId='+ectionData[j].videoSourceId+""
							$(".task-part-list").eq(i).append(								
//								'<a href="javascript:;" id="dela" onclick="clickAction('+sectionData[j].videoSourceId +')">'+
								'<a href="javascript:;" id="dela" onclick="clickAction('+sectionData[j].videoSourceId+')">'+	
									'<div class="task-part-item">'+
											'<i class="task-part-icon">'+
											'</i>'+
											'<p class="task-part-msg">'+
											'<span class="chapter">'+
											'</span>'+
											'<span class="chapter-name">'+sectionName(i)+":"+sectionData[j].sectionName+
											'</span>'+
											'</p>'+
											'<span class="task-msg">免费'+
											'</span>'+
										'</div>'+
								'</a>'
							);
							
						}
						
					}
					
					$(".price-left-bd:nth-child(2)").append(
				      ' <div class="comment-btn">'+
							'<span>点击加载更多'+
							'</span>'+
						'</div>'
				    );
				    
				$(".price-left-bd:nth-child(2)").children(".task-chapter:nth-child(3)").children(".task-part-list").children("a").children("div").addClass("task-disable").attr("data-task","one");
				$(".price-left-bd:nth-child(2)").children(".task-chapter:nth-child(3)").children(".task-part-list").children("a").children("div").children("span").html("付费");
				$(".price-left-bd:nth-child(2)").children(".task-chapter:nth-child(3)").children(".task-part-list").children("a").children("div").on('click',function(){
					if($(".task-chapter:nth-child(3)").children(".task-part-list").children("a").children("div").attr("data-task") == "one"){
					 	$(".task-chapter:nth-child(3)").children(".task-part-list").children("a").attr("href","javascript:;").removeAttr("target").removeAttr("onclick");
					}
				})
					
			    },
			     error:function(jqXHR){     
			       console.log("发生错误：" + jqXHR.status)  
			    } 
		});
	//})
	
	//$(".evaluation").on('click',function(){			
			$.ajax({ 
				type:"GET",
			    dataType:"json",
			    url:'http://192.168.0.12:8080/elink_video/course/getCourseContent/1',
			    //url:"star.json",
			    success: function(result) {					
					var items=result.data.course;
					//console.log(items[i].score)
					
//					$(".comment-box").before(
//						'<div class="comment-title">'+
//							'<div class="comment-statistics">'+
//								'<span>综合评分 :'+																											
//								'</span>'+
//								'<div class="fivestars" id="fivestars">'+
//									'<div class="autostars" id="autostars" style="width:'+19+'">'+
//									'</div>'+
//								'</div>'+	
////								'<div class="fivestars" id="fivestars">'+
////									'<div class="autostars" id="autostars"></div>'+
////								'</div>'+
//							'</div>'+
//							'<div class="filter-comment-rank">'+
//								'<span>内容使用 :'+
//									'<em>'+items.evaluateA+
//									'</em>'+
//								'</span>'+
//								'<span>简单易懂 :'+
//									'<em>'+items.evaluateB+
//									'</em>'+
//								'</span>'+
//								'<span>逻辑清晰 :'+
//									'<em>'+items.evaluateC+
//									'</em>'+
//								'</span>'+
////								'<span class="comment-num">评论('+
////								'<em>'+items.evaluateAmount+
////								'</em>'+
////								')'+
////								'</span>'+
//							'</div>'+
//						'</div>'
//					);
										
					if($(".comment-tips-msg .totalScores").html()>8 && $(".comment-tips-msg.totalScores").html()<10){
						$(".comment-statistics #showb").css("background-position","0px 0px")
						$(".comment-statistics #showb").css("left","0px");
					}else if($(".totalScore").html()>6 && $(".totalScore").html()<8){
						$(".comment-statistics #showb").css("background-position","0px 0px");
						$(".comment-statistics #showb").css("left","-16px");
					}else if($(".totalScore").html()>4 && $(".totalScore").html()<6){
						$(".comment-statistics #showb").css("background-position","0px 0px");
						$(".comment-statistics #showb").css("left","-48px");
					}else if($(".totalScore").html()>1 && $(".totalScore").html()<4){
						$(".comment-statistics #showb").css("background-position","0px 0px");
						$(".comment-statistics #showb").css("left","-64px");
					}else{
						$(".comment-statistics #showb").css("background-position","0px 0px");
						$(".comment-statistics #showb").css("left","-48px");
					}
					
			    },
			     error:function(jqXHR){     
			       console.log("发生错误：" + jqXHR.status)  
			    } 
		});
		
			$.ajax({ 
				type:"GET",
			    dataType:"json",
			    url:'http://192.168.0.12:8080/elink_video/evaluate/getCourseEvaluate/1',
			    //url:"satr1.json",
			    success: function(result) {
			    	//console.log(.score);
					var items=result.data;
					for(var i=0;i<items.length;i++){
						
						$(".comment-msg").before(
							'<div class="comment-tips clearfix">'+
							'<div class="comment-user fl">'+
								'<a class="comment-tips-user">'+
								'<img src="http://192.168.0.12:8080/elink_video/'+items[i].logo+'"/>'+
								'</a>'+
								'<span>'+items[i].nickname+
								'</span>'+
							'</div>'+
							'<div class="comment-tips-msg fl">'+								
								'<div class="fivestars" id="fivestars">'+
									'<div class="autostars" id="autostars" style="width:'+10+'">'+
//									'<div class="autostars" id="autostars" style="width:'+stars(items.score)+'">'+
									'</div>'+
								'</div>'+
								'<div class="comment-tips-title">'+items[i].evaluateContent+
								'</div>'+
								'<ul class="comment-part">'+
									'<li class="comment-list">'+
										'<span class="time">'+getLocalTime(items[i].createTime)+									
										'</span>'+
//										'<span class="date">15:31</span>'
									'</li>'+
								'</ul>'+
							'</div>'+

						'</div>'
						);						
					}

				},
			     error:function(jqXHR){     
			       console.log("发生错误：" + jqXHR.status)  
			    } 
		});
	//})
		
		$.ajax({ 
				type:"GET",
			    dataType:"json",
			    url:'http://192.168.0.12:8080/elink_user/user/getUserInfo/2b54e719b2ad46a193a2851849ba3451',
			    success: function(result) {			    	
			    	//console.log(.score);
					var items=result.data;					
						$(".course-price").append(
						'<div class="price-right fl">'+
						'<h4 class="">个人介绍</h4>'+
						'<div class="user-info clearfix">'+
								'<a href="javascript:;">'+
								'<img src="http://192.168.0.12:8080/elink_video/'+items.logo+'"/>'+
								'</a>'+
								'<div class="user-info-right fl">'+
									'<div class="user-msg">'+
										'<a href="" class="user">'+items.nickname+
										'</a>'+
										'<a class="follow">+ 关注</a>'+
									'</div>'+
									'<span class="video">视频 <em>9</em></span>'+
									'<span class="fans">粉丝 <em>199</em></span>'+
								'</div>'+
						'</div>'+
						'<div class="user-info-tip">'+
						'<p>'+items.describes+
						'</p>'+
						'</div>'+
						'</div>'
						);
					
			    }
				,
			     error:function(jqXHR){     
			       console.log("发生错误：" + jqXHR.status)  
			    }
		});
		
		
		
		//已关注
	var $follow=1;
	$("body").on('click','.follow',function(){
		if($follow==1){
			$(this).html("已关注").css({"background":"#ececec"});
			$(this).css("color","#666");
			$follow++;
		}else if($follow!=1){		
			$(this).html("+ 关注").css({"background":"#49b4e1"});
			$(this).css("color","#fff");
			$follow=1;
		}
	})

})
